package logic

import (
	"context"

	"jinrigaoping/rpc/sys/internal/svc"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysRoleMenuListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysRoleMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysRoleMenuListLogic {
	return &SysRoleMenuListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysRoleMenuListLogic) SysRoleMenuList(in *sys.SysRoleMenuListReq) (*sys.SysRoleMenuListRes, error) {

	menuList, err := l.svcCtx.SysMenuModel.FindMenuListByRoleId(l.ctx, in.RoleId)
	if err != nil {
		return nil, err
	}

	if len(menuList) == 0 {
		return &sys.SysRoleMenuListRes{List: make([]*sys.SysMenuInfoRes, 0)}, nil
	}

	var list = make([]*sys.SysMenuInfoRes, len(menuList))
	for i, v := range menuList {
		list[i] = &sys.SysMenuInfoRes{
			MenuId:      v.MenuId,
			ParentId:    v.ParentId,
			Name:        v.Name,
			Component:   v.Component,
			IsLink:      v.IsLink == 1,
			MenuSort:    v.MenuSort,
			Path:        v.Path,
			Redirect:    v.Redirect,
			Title:       v.Title,
			Icon:        v.Icon,
			IsHide:      v.IsHide == 1,
			IsKeepAlive: v.IsKeepAlive == 1,
			IsAffix:     v.IsAffix == 1,
			LinkUrl:     v.LinkUrl,
			MenuType:    v.MenuType,
			IsIframe:    v.IsIframe == 1,
		}
	}

	return &sys.SysRoleMenuListRes{
		List: list,
	}, nil
}
